package org.peekmoon.meteon;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLCapabilitiesImmutable;
import javax.media.opengl.GLProfile;

import com.jogamp.newt.Display;
import com.jogamp.newt.NewtFactory;
import com.jogamp.newt.Screen;
import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.util.FPSAnimator;

public class MainWindow {
	
	private final GLWindow glWindow;

	protected MainWindow(GLWindow glWindow) {
		this.glWindow = glWindow;
		
		 glWindow.addWindowListener(new WindowAdapter() {
	            @Override
	            public void windowDestroyNotify(WindowEvent arg0) {
	                System.exit(0);
	            }
	        ;
	        });
		 MainEventListener eventListener = new MainEventListener();
		 glWindow.addGLEventListener(eventListener);
		 glWindow.addMouseListener(eventListener);
	}

	public static MainWindow create() {
		final int screenIdx = 0;
        final GLProfile glp = GLProfile.getDefault();
        final GLCapabilities caps = new GLCapabilities(glp);
        Display dpy = NewtFactory.createDisplay(null);
        Screen screen = NewtFactory.createScreen(dpy, screenIdx);
        GLWindow glWindow = GLWindow.create(screen, (GLCapabilitiesImmutable) caps);
        return new MainWindow(glWindow);
	}

	public void setSize(int width, int height) {
		glWindow.setSize(width, height);
		
	}

	public void setVisible(boolean b) {
		glWindow.setVisible(b);
	}

	public void startRender() {
        FPSAnimator animator = new FPSAnimator(glWindow, 60);
        animator.start();
		
	}

}
